<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Category;
use App\Models\Tag;
use App\Models\Post;
use Carbon\Carbon;

class BlogSeeder extends Seeder
{
    public function run(): void
    {
        // 创建管理员用户
        $admin = User::create([
            'name' => '管理员',
            'username' => 'admin',
            'email' => 'admin@example.com',
            'password' => bcrypt('password'),
            'role' => 'admin',
            'status' => 'active',
        ]);

        // 创建普通用户
        $author = User::create([
            'name' => '作者',
            'username' => 'author',
            'email' => 'author@example.com',
            'password' => bcrypt('password'),
            'role' => 'author',
            'status' => 'active',
        ]);

        // 创建分类
        $categories = [];
        $categoryNames = ['技术', 'PHP', 'Laravel', '前端', 'Vue.js', '生活', '随笔'];

        foreach ($categoryNames as $name) {
            $categories[] = Category::create([
                'name' => $name,
                'slug' => \Str::slug($name),
                'description' => $name . '相关的文章',
                'is_active' => true,
            ]);
        }

        // 创建标签
        $tags = [];
        $tagNames = ['编程', 'Web开发', '后端', '前端', '数据库', 'API', '框架', '教程', '经验分享'];

        foreach ($tagNames as $name) {
            $tags[] = Tag::create([
                'name' => $name,
                'slug' => \Str::slug($name),
                'description' => $name . '相关标签',
            ]);
        }

        // 创建文章
        $posts = [
            [
                'title' => 'Laravel 12 新特性介绍',
                'content' => '<p>Laravel 12 带来了许多令人兴奋的新特性，让我们一起来了解一下。</p><p>首先，性能得到了显著提升...</p>',
                'excerpt' => 'Laravel 12 带来了许多令人兴奋的新特性，性能得到了显著提升。',
                'category_id' => $categories[2]->id,
                'is_featured' => true,
                'is_sticky' => true,
            ],
            [
                'title' => 'Vue 3 Composition API 完全指南',
                'content' => '<p>Vue 3 的 Composition API 为我们提供了更灵活的组件编写方式。</p><p>本文将详细介绍如何使用...</p>',
                'excerpt' => 'Vue 3 的 Composition API 为我们提供了更灵活的组件编写方式。',
                'category_id' => $categories[4]->id,
                'is_featured' => true,
            ],
            [
                'title' => 'PHP 8.3 新功能详解',
                'content' => '<p>PHP 8.3 版本带来了更多实用的功能和改进。</p><p>让我们看看都有哪些新特性...</p>',
                'excerpt' => 'PHP 8.3 版本带来了更多实用的功能和改进。',
                'category_id' => $categories[1]->id,
            ],
            [
                'title' => '如何构建现代化的Web应用',
                'content' => '<p>现代化的Web应用需要考虑很多方面，包括性能、安全性、用户体验等。</p>',
                'excerpt' => '现代化的Web应用需要考虑很多方面，包括性能、安全性、用户体验等。',
                'category_id' => $categories[0]->id,
                'is_featured' => true,
            ],
            [
                'title' => '程序员的生活感悟',
                'content' => '<p>作为一名程序员，我想分享一些工作和生活中的感悟。</p>',
                'excerpt' => '作为一名程序员，我想分享一些工作和生活中的感悟。',
                'category_id' => $categories[5]->id,
            ]
        ];

        foreach ($posts as $postData) {
            $post = Post::create([
                'user_id' => $admin->id,
                'category_id' => $postData['category_id'],
                'title' => $postData['title'],
                'slug' => \Str::slug($postData['title']),
                'excerpt' => $postData['excerpt'],
                'content' => $postData['content'],
                'status' => 'published',
                'published_at' => Carbon::now()->subDays(rand(1, 30)),
                'views_count' => rand(10, 1000),
                'likes_count' => rand(0, 50),
                'comments_count' => rand(0, 10),
                'is_featured' => $postData['is_featured'] ?? false,
                'is_sticky' => $postData['is_sticky'] ?? false,
            ]);

            // 为文章添加标签
            $post->tags()->attach(collect($tags)->random(rand(1, 3))->pluck('id'));
        }
    }
}